package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.util.DpUtils;

/**
 * Created by pengmi on 2022/3/4 9:23
 * Description:
 */
public class TxtAnwerStatisticView extends RelativeLayout {

    public TxtAnwerStatisticView(Context context) {
        super(context);
        initView(context);
    }

    public TxtAnwerStatisticView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TxtAnwerStatisticView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.ev_view_txt_answer_statistics,this);
    }

    private void setItem(Item item){
        String stuAnswer=item.getStuAnswer();
        TextView tv_answerCount=findViewById(R.id.tv_answer_count);
        SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
        spannableStringBuilder.append("Word Count:");
        int count=0;
        if(!TextUtils.isEmpty(stuAnswer)){
            String[] split = stuAnswer.split(" ");
            count=split.length;
        }
        EditText editText = findViewById(R.id.edittext);
        editText.setEnabled(false);
        editText.setText(stuAnswer);
        editText.setMinHeight(0);
        SpannableString spannableString=new SpannableString(""+count);
        spannableString.setSpan(new ForegroundColorSpan(0xff666666),0,spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(spannableString);
        tv_answerCount.setText(spannableStringBuilder);
        int dp12= DpUtils.dip2px(getContext(),12);
        LayoutParams layoutParams = (LayoutParams) tv_answerCount.getLayoutParams();
        layoutParams.setMarginStart(0);
        layoutParams.setMarginEnd(0);
        layoutParams.topMargin=dp12;
        tv_answerCount.setLayoutParams(layoutParams);
        LayoutParams layoutParams1 = (LayoutParams) editText.getLayoutParams();
        layoutParams1.width=LayoutParams.MATCH_PARENT;
        layoutParams1.height=LayoutParams.WRAP_CONTENT;
        layoutParams1.bottomMargin=0;
        editText.setLayoutParams(layoutParams1);
    }
}
